﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace YellowMap.View.Control
{
    public partial class DirectionListItem : UserControl
    {
        public ListBox ListBox { get; set; }
        public List<LocationPoint> Points { get; set; }
        public WhiteNodePin Pin { get; set; }
        public Map BingMap { get; set; }

        string BLACK = "/../../Resources/Icon/black/";
        string WHITE = "/../../Resources/Icon/white/";
        public string Icon;
        public static DirectionListItem SelectedDirectionListItem { get; set; }

        public DirectionListItem()
        {
            InitializeComponent();
            LoadIcon();
            LoadTheme();
        }

        private void LoadIcon()
        {
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
            {
                Icon = "/../../Resources/Icon/white/";
            }
            else
                Icon = "/../../Resources/Icon/black/";
        }
        private void LoadTheme()
        {
            txtDistance.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            txtText.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
        }

        public void Init(string distance, string text, WhiteNodePin pin)
        {
            txtDistance.Text = distance;
            txtText.Text = text;
            Pin = pin;
        }

        DirectionIcon directionIcon = DirectionIcon.NONE;
        string icon_Path = "";
        public void RecognizeIcon()
        {

            string lang = AppViewModel.Instance.SettingVM.Language;
            if (lang == "Vi")
            {
                directionIcon = Common.RegconizeDirectionIcon_Vi(txtText.Text);
            }
            else
            {
                directionIcon = Common.RegconizeDirectionIcon_En(txtText.Text);
            }

            switch (directionIcon)
            {
                case DirectionIcon.GO_STRAIGHT:
                    icon_Path = "direction_none.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_none.png", UriKind.Relative));
                    break;
                case DirectionIcon.U_TURN_LEFT:
                    icon_Path = "direction_turn_left.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_turn_left.png", UriKind.Relative));
                    break;
                case DirectionIcon.U_TURN_RIGHT:
                    icon_Path = "direction_turn_right.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_turn_right.png", UriKind.Relative));
                    break;
                case DirectionIcon.TURN_LEFT:
                    icon_Path = "direction_turn_left.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_turn_left.png", UriKind.Relative));
                    break;
                case DirectionIcon.TURN_RIGHT:
                    icon_Path = "direction_turn_right.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_turn_right.png", UriKind.Relative));
                    break;
                case DirectionIcon.KEEP_LEFT:
                    icon_Path = "direction_keep_left.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_keep_left.png", UriKind.Relative));
                    break;
                case DirectionIcon.KEEP_RIGHT:
                    icon_Path = "direction_keep_right.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_keep_right.png", UriKind.Relative));
                    break;
                case DirectionIcon.ROUNGABOUT_LEFT:
                    icon_Path = "direction_around_left.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_around_left.png", UriKind.Relative));
                    break;
                case DirectionIcon.ROUNDABOUT_RIGHT:
                    icon_Path = "direction_around_right.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_around_right.png", UriKind.Relative));
                    break;
                case DirectionIcon.SLIGHT_LEFT:
                    icon_Path = "direction_slight_left.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_slight_left.png", UriKind.Relative));
                    break;
                case DirectionIcon.SLIGHT_RIGHT:
                    icon_Path = "direction_slight_right.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_slight_right.png", UriKind.Relative));
                    break;
                case DirectionIcon.SHARP_LEFT:
                    icon_Path = "direction_sharp_left.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_sharp_left.png", UriKind.Relative));
                    break;
                case DirectionIcon.SHARP_RIGHT:
                    icon_Path = "direction_sharp_right.png";
                    this.icon.Source = new BitmapImage(new Uri(Icon + "direction_sharp_right.png", UriKind.Relative));
                    break;
                default:
                    // this.icon.Source = new BitmapImage(new Uri(WHITE + "direction_none.png", UriKind.Relative));
                    break;
            }
        }

        bool _IsSelected;
        public bool IsSelected
        {
            get
            {
                return _IsSelected;

            }

            set
            {
                _IsSelected = value;
                if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                {
                    if (value)
                    {
                        if (SelectedDirectionListItem != null)
                        {
                            SelectedDirectionListItem.IsSelected = false;
                        }

                        VisualStateManager.GoToState(this, "selected", true);
                        Pin.ChangeToSelectedState();
                        if (icon_Path != "")
                        {
                            this.icon.Source = new BitmapImage(new Uri(BLACK + icon_Path, UriKind.Relative));
                        }


                        BingMap.SetView(new System.Device.Location.GeoCoordinate(Pin.Data.Lattitude, Pin.Data.Longitude), Constant.MAP_ZOOM_LEVEL_DEFAULT, MapAnimationKind.Linear);
                        ListBox.SelectedItem = this;
                        ListBox.ScrollIntoView(this);
                        SelectedDirectionListItem = this;
                    }
                    else
                    {
                        if (icon_Path != "")
                        {
                            this.icon.Source = new BitmapImage(new Uri(WHITE + icon_Path, UriKind.Relative));
                        }
                        VisualStateManager.GoToState(this, "normal", true);
                        Pin.ChangeToNormalState();
                    }
                }
                else
                {
                    if (value)
                    {
                        if (SelectedDirectionListItem != null)
                        {
                            SelectedDirectionListItem.IsSelected = false;
                        }

                        VisualStateManager.GoToState(this, "selectedLight", true);
                        Pin.ChangeToSelectedState();
                        if (icon_Path != "")
                        {
                            this.icon.Source = new BitmapImage(new Uri(WHITE + icon_Path, UriKind.Relative));
                        }


                        BingMap.SetView(new System.Device.Location.GeoCoordinate(Pin.Data.Lattitude, Pin.Data.Longitude), Constant.MAP_ZOOM_LEVEL_DEFAULT);
                        ListBox.SelectedItem = this;
                        ListBox.ScrollIntoView(this);
                        SelectedDirectionListItem = this;
                    }
                    else
                    {
                        if (icon_Path != "")
                        {
                            this.icon.Source = new BitmapImage(new Uri(BLACK + icon_Path, UriKind.Relative));
                        }
                        VisualStateManager.GoToState(this, "normal", true);
                        Pin.ChangeToNormalState();
                    }
                }
            }
        }

        private void LayoutRoot_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //  IsSelected = true;
        }

        public DirectionIcon Ultility { get; set; }

        private void txtText_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            double newHeight = e.NewSize.Height;
            if (newHeight > this.ActualHeight)
            {
                this.Height = newHeight + 5;
               // LayoutRoot.Height = newHeight + 5;
            }
        }
    }
}
